<?php

namespace App\Manage;


use App\Models\SiteInfo;

class SiteInfoManage extends Manage
{

    // TODO 单页设置


    // 获取当前域名
    public function domain()
    {
        $opt     = $this->option();
        $domain  = $this->request()->server('HTTP_HOST');
        $isHttps = $this->request()->server('HTTPS');

        $domain = ($isHttps ? 'https://' : 'http://').$domain;

        return $domain;
    }

    // 当前cdn域名
    public function cdn()
    {
        $opt    = $this->option();
        $domain = $opt->domain('oss');

        return $domain;
    }

    // 获取配置
    public function lists($myUser, $options = [])
    {
        $model = new SiteInfo();
        $list  = $model->getList([['key','<>','']], 0, 'id DESC');

        return $list;
    }

    // 内容
    public function content($myUser, $options = [])
    {


        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt = $this->option();
        $key = $opt->getData('key', null, 'trim');
        if(empty($key)){
            //return $this->_returnFail('请选择你要操作的数据！');
            return $this->_returnFail('please_select_the_data_you_wish_to_operate');
        }

        $model = new SiteInfo();
        $info  = $model->oneFind(['key' => $key]);
        if($info === false){
            return $this->_returnFail($model->getError());
        }

        return $info;
    }

    // 保存
    public function save($myUid, $options = [])
    {
        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt = $this->option();
        $key = $opt->getData('key', null, 'trim');
        if(empty($key)){
            //return $this->_returnFail('请指定查看记录！');
            return $this->_returnFail('select_operation_id');
        }
        $data = [
            'title'   => $opt->getData('title', null, 'trim'),
            'content' => $opt->getData('content', null, 'trim'),
            'status'  => $opt->getData('status', 1, 'intval'),
        ];

        // 文章内容处理
        if($data['content']){
            $data['content'] = htmlspecialchars_decode($data['content']);
            $data['content'] = str_replace("<br/>", "</p><p>", $data['content']);
            $data['content'] = str_replace("<p><br/></p>", "", $data['content']);

        }
        // 设置 content
        if($data['content']){
            $model = new SiteInfo();
            // 设置
            $data['content'] = $model->setContentAttribute($data['content']);
        }

        // 模型
        $model = new SiteInfo();
        $data  = $model->filterField($data, true);
        if($data === false){
            return $this->_returnFail($model->getError());
        }
        if(empty($data)){
            //return $this->_returnFail('不能修改为空！');
            return $this->_returnFail('send_data_to_be_modify');
        }

        // 执行
        $result = $model->oneEdit(['key' => $key], $data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 管理员日志
        /*if($is_admin_log){
            $this->addAdminLog($myUser['id'], '修改自己的个人资料', '');
        }*/

        return true;
    }


}


